﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Majoopic.DataAccess.Model;

namespace Majoopic.UnitTest
{
    [TestClass]
    public class SQLDataAccessTest
    {
        //public const string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=MajoopicDB.mdf;Integrated Security=True;User Instance=True";
        [TestMethod]
        public void AddPicture()
        {
            Picture picture = this.CreateHDDPicture();
            using (var dbContext = new MajoopicEntities())
            {
                dbContext.Pictures.Add(picture);
                dbContext.SaveChanges();
            }
        }
        
        private Picture CreateHDDPicture()
        {
            Picture picture = new Picture();
            picture.Deleted = false;
            picture.FileName = "IMG_1234.jpg";
            picture.LocalFolder = this.CreateCompleteFolder();
            picture.PictureDetail = this.CreatePictureDetail();
            picture.PictureLogs.Add(this.CreatePictureLog());
            picture.Source = Constants.SourceHDD;
            return picture;            
        }

        private PictureLog CreatePictureLog()
        {
            PictureLog pictureLog = new PictureLog();
            pictureLog.Action = Constants.LogActionAdd;
            pictureLog.Comment = "Add a test image";
            pictureLog.LogDateTime = DateTime.Now;
            return pictureLog;
        }

        private PictureDetail CreatePictureDetail()
        {
            PictureDetail pictureDetail = new PictureDetail();
            pictureDetail.CameraModel = "Nikon D90";
            pictureDetail.HashCode = "ABVCZF564SG356GED";
            pictureDetail.DateTaken = new DateTime(2013, 02, 18, 13, 21, 42);
            pictureDetail.Deleted = false;
            pictureDetail.Extension = "JPG";
            pictureDetail.FileSize = 2123456789;
            pictureDetail.Height = 768;
            pictureDetail.Width = 1024;
            pictureDetail.LensModel = "Nikon 18/200mm VRII";
            return pictureDetail;
        }

        private LocalFolder CreateCompleteFolder()
        {
            LocalFolder rootFolder = new LocalFolder();
            rootFolder.Device = this.CreateDevice();
            rootFolder.FolderName = "D:";            
            LocalFolder childFolder = this.AddChildFolder(rootFolder, this.CreateLocalFolder("Alex", rootFolder));
            childFolder = this.AddChildFolder(childFolder, this.CreateLocalFolder("Photo", childFolder));
            childFolder = this.AddChildFolder(childFolder, this.CreateLocalFolder("2013", childFolder));
            childFolder = this.AddChildFolder(childFolder, this.CreateLocalFolder("Australia", childFolder));
            childFolder = this.AddChildFolder(childFolder, this.CreateLocalFolder("Jpg", childFolder));
            return rootFolder;
        }

        private LocalFolder AddChildFolder(LocalFolder rootFolder, LocalFolder childFolder)
        {
            rootFolder.ChildFolders.Add(childFolder);
            return childFolder;
        }

        private LocalFolder CreateLocalFolder(string name, LocalFolder rootFolder)
        {
            LocalFolder localFolder = new LocalFolder();
            localFolder.Deleted = false;
            localFolder.FolderName = name;            
            return localFolder;
        }

        private Device CreateDevice()
        {
            Device device = new Device();
            device.DeviceName = "Laptop";
            return device;
        }
    }
}
